package com.example.laixunlong.control;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.laixunlong.R;

public class EditText_shuru extends AppCompatActivity
{
    public EditText shuru1,shuru2;
    public Button but1,but2;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext_shuru);
        but1 = findViewById(R.id.input_button_1);
        shuru1=findViewById(R.id.input_1);
    /*普通文本*/
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a= shuru1.getText().toString();
                Log.e("输入框获取内容:", a);
            }
        });

    /*密码*/
        but2=findViewById(R.id.input_button_2);
        shuru2=findViewById(R.id.input_2);
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a= shuru2.getText().toString();
                Log.e("输入的密码内容:", a);
            }
        });
        //焦点监听,用于控制文本内容,主要原因是输入框控件需要点击两次才能触发点击事件,第一次点击只能获取到焦点
        shuru2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus)
                {   //如果密码长度小于10
                    if(shuru1.getText().toString().length()<3)
                    {
                        shuru1.requestFocus();
                        String a= shuru1.getText().toString();
                        Log.e("焦点监听到用户名",a);
                        Toast.makeText(EditText_shuru.this,"用户名不小于3位",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //文本监听
        shuru2.addTextChangedListener(new textMonitor(shuru2,6));

    }
private class textMonitor implements TextWatcher
{
    private EditText input_item;
    private int length;
    textMonitor(EditText i,int l)
    {
        this.input_item=i;this.length=l;
    }
    @Override/*内容改变前,   点击软键盘,但内容未写入输入框时*/
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }
    @Override/*内容改变时,   io流向输入框传递文本时*/
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }
    @Override/*内容改变后,   文本变更的io流关闭时*/
    public void afterTextChanged(Editable s) {
        if(s.toString().length()<length)
        {
            Log.e("获取到数据",s.toString());
        }
        else//超过6位关闭键盘
        {
            Log.e("获取到数据",s.toString());
            InputMethodManager im=(InputMethodManager) EditText_shuru.this.getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(input_item.getWindowToken(),0);
        }
    }
}

}
